package com.atguigu.exceptionhandler;

import com.atguigu.commonutils.CommonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author : 李红磊
 * @Date : 2021/06/03/23:20
 * @Description :
 */
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(Exception.class)//全局异常
    @ResponseBody
    public CommonResult error(Exception e){
        e.printStackTrace();
        return CommonResult.error().message("执行了全局异常");
    }


    @ExceptionHandler(ArithmeticException.class)//特定异常
    @ResponseBody
    public CommonResult error2(ArithmeticException e){
        e.printStackTrace();
        return CommonResult.error().message("执行了特定异常");
    }



    @ExceptionHandler(GuliException.class)//自定义异常
    @ResponseBody
    public CommonResult error3(GuliException e){
        e.printStackTrace();
        return CommonResult.error().code(e.getCode()).message(e.getMsg());
    }
}
